#include <QWheelEvent>
#include <QDebug>
#include <QMenu>
#include <QCursor>

#include "scrollline.h"


ScrollLine::ScrollLine()
{
    m_index=0;
    this->setReadOnly(true);
}

ScrollLine::ScrollLine(const QList<QString>& list, const QString& text)
{
    this->setText(text);
    this->setList(list);

    this->setReadOnly(true);
}
ScrollLine::~ScrollLine()
{
    /* Everything destructs itself at the present.*/
}
void ScrollLine::setList(const QList<QString>& list)
{
    m_list=&list;

    m_index = m_list->indexOf(this->displayText());
    if ( m_index < 0 )
    {
        ; // Error can go here if you really want it.
    }
}
void ScrollLine::wheelEvent(QWheelEvent *event)
{
    /*Delta returns a number in 1/8th of a degree, and 15 degrees is one
     * "scroll" on a mouse wheel. Therefore 120*oneMove = Delta.
     * Then, increment the index by the distance scrolled.
     */
    /* Note, this function doesn't work for those of us that have accelerated
        track pads, and anyone that doesn't have a wheel on their mouse.
     */
    int oneMove=event->delta()/120.0;
    m_index+= oneMove;
    if(m_index >= m_list->size()-1)
        m_index=0;
    if(m_index < 0)
        m_index=m_list->size()-2;

    this->setText(m_list->at(m_index));

    editingFinished();
}

void ScrollLine::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton)
    {
        QMenu* menu = this->createStandardContextMenu();

        menu->addSeparator();
        /* Rewrite rule saving to go along
         * with this. Lots of fun :)
         */
        QAction* clear = menu->addAction("Clear");
        connect(clear, SIGNAL(triggered()), this, SLOT(setEmpty()));

        menu->exec(QCursor::pos());

    }
    else
    {
        m_index++;
        if(m_index >= m_list->size()-1)
            m_index=0;
         this->setText(m_list->at(m_index));
    }
    editingFinished();
}

void ScrollLine::setEmpty()
{
    m_index = m_list->size()-1;
    this->setText(m_list->at(m_index));
}
